﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtBusiness.OnLine;
using System.Data;
using ExtBusiness;
using System.Web.Security;
using System.Drawing;
using ExtBusiness.FlowOperation;

public partial class office_MyShenHeResult : PageBase
{
    private RequestFlowOperation rfoGloab = new RequestFlowOperation();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlTjlb.DataBind();
            BindFlowName();
            BindMyHandled();            
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    protected DataTable GetDataSource()
    {
        string strWhere = string.Empty;
        if (!string.IsNullOrEmpty(txtIID.Text))//受理编号
            strWhere += string.Format(" and slbh like '%{0}%'", txtIID.Text.Trim());
        if (!string.IsNullOrEmpty(txtName.Text.Trim()))//申请单位
            strWhere += string.Format(" and i.name like '%{0}%'", txtName.Text.Trim());
        if (!string.IsNullOrEmpty(ddlFlow.SelectedItem.Value))//流程名称
            strWhere += string.Format(" and w.wname = '{0}'", ddlFlow.SelectedItem.Value);
        if (!string.IsNullOrEmpty(txtSth.Text))//审图号
            strWhere += string.Format(" and t.批准号 like '%{0}%'", txtSth.Text);
        if (!string.IsNullOrEmpty(startDate.Value))//开始时间
            strWhere += string.Format(" and i.accepted_time >=to_date('{0}','yyyy-mm-dd')", startDate.Value);
        if (!string.IsNullOrEmpty(endDate.Value))//结束时间
            strWhere += string.Format(" and i.accepted_time <= to_date('{0}','yyyy-mm-dd')", endDate.Value);
        if (!string.IsNullOrEmpty(ddlTjlb.Text))//图件类别
            strWhere += string.Format(" and dtyt='{0}'", ddlTjlb.Text);
        if (!string.IsNullOrEmpty(txtTm.Text))//图名
            strWhere += string.Format(" and 图名 like '%{0}%'", txtTm.Text);
        if (!string.IsNullOrEmpty(txtShr.Text))//审核人
            strWhere += string.Format(" and exists (select 编号 from sv_work_item_list k where 经办人 like '%{0}%' and i.iid=k.编号)", txtShr.Text);

        if (!string.IsNullOrEmpty(calGwStart.Value) || !string.IsNullOrEmpty(calGwEnd.Value))
        {
            strWhere +=  @" and i.iid in (select h.iid from st_work_item_hist h where h.submit_time is not null  ";

            if (!string.IsNullOrEmpty(calGwStart.Value))//开始时间
                strWhere += string.Format(" and h.submit_time>=to_date('{0}','yyyy-mm-dd hh24-mi-ss')", calGwStart.Value);
            if (!string.IsNullOrEmpty(calGwEnd.Value))//结束时间
                strWhere += string.Format(" and h.submit_time<=to_date('{0}','yyyy-mm-dd hh24-mi-ss')", calGwEnd.Value);

            strWhere += ")";
        }

        if (!string.IsNullOrEmpty(gsCcqk.Text))//抽检
        {
            strWhere += string.Format(" and t.ccqk='{0}'", gsCcqk.Text);
        }
        if (!string.IsNullOrEmpty(gsBaqk.Text))//备案
        {
            strWhere += string.Format(" and t.baqk='{0}'", gsBaqk.Text);
        }
        if (cbxTh.Checked)//退改
        {
            //strWhere += string.Format(" and c.IS_TG='1'");
            strWhere += " and i.iid in (select iid from UT_地图内容审查意见书 where 审查意见 like '%不符合%有关规定。')";
        }
        if (cbxCss.Checked)//曾送审
        {
            strWhere += string.Format(" and c.IS_CSS='1'");
        }
        if (cbxGd.Checked)//归档
        {
            strWhere += string.Format(" and c.IS_GD='1'");
        }
        if (!string.IsNullOrEmpty(ddlTtgjz.Text))//归档
        {
            strWhere += string.Format(" and i.iid in (select iid from UT_地图内容审查意见书 where 审查意见 like '%{0}%' and 审查意见 like '%不符合%有关规定。')", ddlTtgjz.Text);
        }

        DataTable dtTemp = rfoGloab.GetMyShenPiResultByUserID("", strWhere);
        return dtTemp;
    }
    
    /// <summary>
    /// 绑定留言咨询信息
    /// </summary>
    /// <!--addby zhongjian 20091103-->
    protected void BindMyHandled()
    {
        //拓展表业务状态(0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
        DataTable dtTemp = GetDataSource();
        
        this.gvMessage.DataSource = dtTemp;
        this.gvMessage.RecordCount = dtTemp.Rows.Count;
        this.gvMessage.DataBind();
    }

    public string FormatInfo(string strValue)
    {
        if( !string.IsNullOrEmpty(strValue))
        {
            return strValue;
        }
        else
        {
            return "无";
        }
    }

    /// <summary>
    /// datagrid数据加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_OnLoadData(object sender, EventArgs e)
    {
        this.BindMyHandled();
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        BindMyHandled();
    }

    /// <summary>
    /// 绑定该用户有权限的流程名称
    /// </summary>
    /// <!--addby zhongjian 20100608-->
    public void BindFlowName()
    {
        ClsWorkFlow newWorkflow = new ClsWorkFlow();
        DataTable dtTable = newWorkflow.GetWorkFlowByUserId(UserId, "");
        ddlFlow.Items.Clear();
        ddlFlow.Items.Add(new ListItem("所有流程", ""));
        foreach (DataRow row in dtTable.Rows)
        {
            ddlFlow.Items.Add(new ListItem(row["wname"].ToString(), row["wname"].ToString()));
        }
    }

    protected void gvMessage_OnImportClick(object sender, EventArgs e)
    {
        DataTable dtTemp = GetDataSource();
        if (dtTemp.Rows.Count > 0)
        {
            dtTemp.Columns["iid"].ColumnName = "办件编号";
            
            dtTemp.Columns["wname"].ColumnName = "办件类型";            
            dtTemp.Columns["dtyt"].ColumnName = "图件类型";
            dtTemp.Columns["tm"].ColumnName = "图名";
            dtTemp.Columns["slh"].ColumnName = "受理编号";
            dtTemp.Columns["name"].ColumnName = "申请单位";
            dtTemp.Columns["accepted_time"].ColumnName = "受理时间";

            dtTemp.Columns["BAQK"].ColumnName = "备案情况";
            dtTemp.Columns["CCQK"].ColumnName = "抽检情况";
            dtTemp.Columns["SJBARQ"].ColumnName = "实际备案日期";
            dtTemp.Columns["CCSJ"].ColumnName = "抽查日期";

            if (dtTemp.Columns.Contains("PRIORITY"))
            {
                dtTemp.Columns.Remove("PRIORITY");
            }
            if (dtTemp.Columns.Contains("STATUS"))
            {
                dtTemp.Columns.Remove("STATUS");
            }

            if (!string.IsNullOrEmpty(txtShr.Text))
            {
                dtTemp.Columns.Add("审查人");
                foreach (DataRow drTemp in dtTemp.Rows)
                {
                    drTemp["审查人"] = txtShr.Text;
                }
            }

            Common.ExcelOperation.TableToExcelCustom(dtTemp, Guid.NewGuid().ToString());
        }
    }

}
